//
//  PKBaseTabBarController.swift
//  Panku
//
//  Created by mac on 2017/2/6.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit

class PKBaseTabBarController: UITabBarController {
    
    static var sharedInstance:PKBaseTabBarController?
    
    //MARK: properties
    var viewModel:PKBaseTabBarViewModel?
    
    //tabbar
    var customTabBar:PKBaseTabBar?
    
    //MARK: init
    init(viewModel:PKBaseTabBarViewModel) {

        self.viewModel = viewModel
        super.init(nibName: nil, bundle: nil)
        
        PKBaseTabBarController.sharedInstance = self
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupTabItems()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //代码方式加载tabbarController方式下 自定义tabBar不能相应事件 所以下面两行代码只能放在viewWillAppear这里 才可以响应事件
        self.customTabBar?.frame = self.tabBar.bounds
        self.tabBar.addSubview(self.customTabBar!)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private func setupTabItems() -> Void{
        
        //移除所有子views
        for view in self.tabBar.subviews{
            view.removeFromSuperview()
        }
        
        //配置自定义tabBar
        self.customTabBar = PKBaseTabBar(With: { (index) in
            
            //如果点击的是发布和我的tab,而且还未登录则直接弹出
            if (index == 1 || index == 2) && (PKLoginServices.isLogin() == false) {
                let loginViewController = PKLoginViewController(viewModel: self.viewModel!.viewModelForLoginViewController())
                let navController = PKBaseNavigationController(rootViewController: loginViewController)
                self.present(navController, animated: true, completion: {
                    
                })
                return
            }
            
            if index == 1{
                //弹出发布入口页 不切换tab
                PKPublishEntryView.setCallbacks(didClickAtPublishLiveButton: { 
                    let publishLiveController = PKPublishLiveController(viewModel:self.viewModel!.viewModelForPublishLiveController())
                    self.present(publishLiveController, animated: false, completion: {
                        //finish callback
                    })
                }, didClickAtPublsihShortVideoButton: { 

                    let publishShortVideoController = PKPublishShortVideoController(viewModel: self.viewModel!.viewModelForPublishShortVideoViewController())
                    self.present(publishShortVideoController, animated: false, completion: { 
                        //finish callback
                    })
                })
                PKPublishEntryView.showIn()
                return
            }
            
            //切换Tab 控制器页面
            self.selectedIndex = index
        })
    
        //主页
        let homeController = PKBaseNavigationController(rootViewController: PKHomeViewController(viewModel: viewModel!.viewModelForHomeController()))
        
        addController(controller: homeController, image: R.image.tab_home()!, selectedImage: R.image.tab_home_selected()!)
        
        //发布
        let publishController = UIViewController()
        
        addController(controller: publishController, image: R.image.tab_publish()!, selectedImage: R.image.tab_publish()!)
        
        //我的
        let mineController = PKBaseNavigationController(rootViewController: PKMineViewController(viewModel: viewModel!.viewModelForMineController()))
        
        addController(controller: mineController, image: R.image.tab_mine()!, selectedImage: R.image.tab_mine_selected()!)
        
    }
    
    //加入子控制器
    private func addController(controller:UIViewController,image:UIImage,selectedImage:UIImage) -> Void{
        
        self.customTabBar?.addTab(With: image, selectedImage: selectedImage)
        self.addChildViewController(controller)
    }
    
}
